package ar.com.ford.it.fullEconomics.persistence.dao.query;

import oracle.toplink.expressions.Expression;
import oracle.toplink.expressions.ExpressionBuilder;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesc05Itemcatgry;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesg01Plant;
import com.ford.it.persistence.impl.toplink.CustomExpressionFactory;

public class ItemCategoriesFindQuery implements CustomExpressionFactory {

    public Expression createExpression(ExpressionBuilder builder, Object[] parameters) {
        Expression exp = null;
        Teesc05Itemcatgry itemCategory = (Teesc05Itemcatgry) parameters[0];        
        Integer code = null;
        String name = null;
        Teesg01Plant plant = null;
        
        code = itemCategory.getEesc05ItemcatgryNumberK();
        name = itemCategory.getEesc05ItemcatgryNameN();
        plant = itemCategory.getEesc05PlantCodeC();

        if (name == null) {
            name = "%";
        } else {
            name += "%";
        }

        exp = builder.get("eesc05PlantCodeC").equal(plant);
        if (code != null) {
            exp = exp.and(builder.get("eesc05ItemcatgryNumberK").equal(code));
        } else {
            exp = exp.and(builder.get("eesc05ItemcatgryNameN").toLowerCase().like(
                    name.toLowerCase()));
        }

        return exp;
    }

}
